﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Domain.Interface;
using Domain.Model;
using Domain.Model.Domain_MoBan_Model;
using Domain.Model.Domain_Personal;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WebAPI.Attributes;

namespace WebAPI.Controllers
{
    public class KaoHeBiaoCaoZuoController : ApiController
    {
        public IMyKaoHeBiao _IMyKaoHeBiao { set; get; }
        public IDeFenHuiZong _IDeFenHuiZong { set; get; }

        [HttpGet]
        public dynamic CreateMyKaoHebiao(string username, string sssj)
        {
            string str = _IMyKaoHeBiao.CreateMyKaoHeBiao(username, sssj);
            dynamic dy = JsonConvert.DeserializeObject<dynamic>(str);
            return dy;
        }

        [HttpGet]
        public void DelMyKaoHeBiao(int khbid)
        {
            _IMyKaoHeBiao.DelMyKaoHeBiao(khbid);
        }

        [HttpPost]
        public List<Domain_PersonalMain> GetMyKaoHeBiaos(dynamic obj)
        {
            var lst = _IMyKaoHeBiao.GetMyKaoHeBiaos(obj.username.ToString());
            return JsonConvert.DeserializeObject<List<Domain_PersonalMain>>(lst);
        }

        [HttpPost]
        public List<Domain_MyKaoHeBiao> GetLiShiMyKaoHeBiaos(string username)
        {
            var lst = _IMyKaoHeBiao.GetMyLiShiKaoHeBiaos(username);
            return JsonConvert.DeserializeObject<List<Domain_MyKaoHeBiao>>(lst);
        }

        [HttpPost]
        public Domain_PersonalMain GetMyKaoHeBiaoInfo(dynamic obj)
        {
            var model = _IMyKaoHeBiao.GetMyKaoHeBiaoInfo(int.Parse(obj.khbid.ToString()));
            return JsonConvert.DeserializeObject<Domain_PersonalMain>(model);
        }

        [HttpPost]
        public void UpdateMyKaoHeBiao([FromBody] Domain_MyKaoHeBiao model)
        {
            _IMyKaoHeBiao.UpdateMyKaoHeBiao(model);
        }

        /// <summary>
        /// 变更考核表状态
        /// </summary>
        /// <param name="obj"></param>
        [HttpGet]
        public dynamic UpdateMyKaoHeBiaoState(string username, int khbid, int state, string yijian = "")
        {
            var result = _IMyKaoHeBiao.UpdateMyKaoHeBiaoState(username, khbid, state, yijian);

            return JObject.Parse(result);
        }

        [HttpPost]
        public List<Domain_MyKaoHeBiao> GetGongZuoJiHuasBymbid(dynamic obj)
        {
            var lst = _IMyKaoHeBiao.GetGongZuoJiHuasBymbid(int.Parse(obj.mbid.ToString()));
            return JsonConvert.DeserializeObject<List<Domain_MyKaoHeBiao>>(lst);
        }

        [HttpGet]
        public void TongGuoShenHe(string username, int mkhbid)
        {
            _IMyKaoHeBiao.UpdateKaoHeBiaoZhuangTai(username, mkhbid, 4);
        }

        //[HttpGet]
        //public void BoHuiShenHe(int mkhbid)
        //{
        //    _IMyKaoHeBiao.UpdateKaoHeBiaoZhuangTai(mkhbid, 2);
        //}
        /// <summary>
        /// 生成得分表
        /// </summary>
        /// <param name="ksid"></param>
        /// <param name="pcid"></param>
        [HttpGet]
        public void CreateKaoHuiZong(int ksid)
        {
            _IDeFenHuiZong.CreateDeFenBiaoHuiZong(ksid);
        }

        /// <summary>
        /// 获取下属的得分表
        /// </summary>
        /// <param name="zwid"></param>
        /// <returns></returns>
        [HttpGet]
        public List<Domain_KaoHeDeFenHuiZong> GetXiaShuZhiWuYuanGong(string username)
        {
            var lst = _IDeFenHuiZong.GetXiaShuZhiWuYuanGong(username);
            return JsonConvert.DeserializeObject<List<Domain_KaoHeDeFenHuiZong>>(lst);
        }

        [HttpGet]
        public List<Domain_KaoHeDeFenHuiZong> GetKaoHeWeiTiao(string username, int mbid
        )
        {
            var lst = _IDeFenHuiZong.GetKaoHeWeiTiao(username, mbid);
            return JsonConvert.DeserializeObject<List<Domain_KaoHeDeFenHuiZong>>(lst);
        }

        /// <summary>
        /// 确认考核成绩
        /// </summary>
        /// <param name="zwid"></param>
        [HttpGet]
        public void QueRenXiaShuDeFen(string username)
        {
            _IDeFenHuiZong.QueRenXiaShuDeFen(username);
        }

        /// <summary>
        /// 在确认排名之前更新得分表
        /// </summary>
        /// <param name="mkhbid"></param>
        /// <param name="username"></param>
        [HttpGet]
        public void UpdateDeFenBiaoHuiZong(int mkhbid, string username)
        {
            _IDeFenHuiZong.UpdateDeFenBiaoHuiZong(mkhbid, username);
        }

        /// <summary>
        /// 按时间获取考核表
        /// </summary>
        /// <param name="zwid"></param>
        /// <returns></returns>
        [HttpGet]
        public List<Domain_MyKaoHeBiao> GetMyKaoHeBiaoBydatetime(string username, string datetimes)
        {
            var lst = _IMyKaoHeBiao.GetMyKaoHeBiaoBydatetime(username, datetimes); //GetXiaShuZhiWuYuanGong(username);
            return JsonConvert.DeserializeObject<List<Domain_MyKaoHeBiao>>(lst);
        }

        [HttpGet]
        public string GetMyBoHuiYiJian(string username)
        {
            var msg = _IMyKaoHeBiao.GetMyBoHuiYiJian(username); //GetXiaShuZhiWuYuanGong(username);
            return msg;
        }

        [HttpGet]
        public void BoHuiYiJianYiYue(int mkhbid)
        {
            _IMyKaoHeBiao.BoHuiYiJianYiYue(mkhbid);
        }

        [HttpPost, WriteFileExceptionAspect]
        public void CreateLinShiGongZuo(dynamic obj)
        {
            _IMyKaoHeBiao.CreateLinShiGongZuo(obj.username.ToString(), obj.gznr.ToString(), obj.gzbz.ToString(),
                int.Parse(obj.mkhbid.ToString()));
        }

        [HttpGet]
        public void DelLinShiGongZuo(int mkhbid, int twoid)
        {
            _IMyKaoHeBiao.DelLinShiGongZuo(mkhbid, twoid);
        }

        [HttpPost, WriteFileExceptionAspect]
        public void UploadFile(dynamic obj)
        {
            _IMyKaoHeBiao.UploadFile(int.Parse(obj.mkhbid.ToString()), int.Parse(obj.twoid.ToString()),
                obj.fjmc.ToString(), obj.path.ToString(), obj.fileHash.ToString(), obj.fileguid.ToString());
        }

        [HttpGet]
        public void DelUploadFile(int mkhbid, int twoid, int fjid)
        {
            _IMyKaoHeBiao.DelUploadFile(mkhbid, twoid, fjid);
        }

        public List<Domain_MyKaoHeBiaotwo_FuJians> GetFileList(int mkhbid, int twoid)
        {
            string str = _IMyKaoHeBiao.GetFileList(mkhbid, twoid);
            return JsonConvert.DeserializeObject<List<Domain_MyKaoHeBiaotwo_FuJians>>(str);
        }

        //public void addKaoHeZongJie(dynamic obj)
        //{
        //    var zongjietwo = JsonConvert.DeserializeObject<zongjietwo>(obj.zongjietwo.ToString());
        //    _IMyKaoHeBiao.add考核总结内容(zongjietwo, obj.zongjieoneid, obj.username);
        //}

        //public zongjieone getZongjie(string username, int mkhbid)
        //{
        //    string str = _IMyKaoHeBiao.get获取考核总结(username, mkhbid);
        //    return JsonConvert.DeserializeObject<zongjieone>(str);
        //}

        //public void updatezongjie(dynamic obj)
        //{
        //    var zongjietwo = JsonConvert.DeserializeObject<zongjietwo>(obj.zongjietwo.ToString());
        //    _IMyKaoHeBiao.update更新考核总结(obj.zongjieone, zongjietwo, obj.username);
        //}

        //public void delzongjietow(int zongjietowid, string username)
        //{
        //    _IMyKaoHeBiao.del删除考核总结(zongjietowid, username);
        //}

        /// <summary>
        /// 获取工作承担人
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost, WriteFileExceptionAspect]
        public List<dynamic> getGongZuoChengDanRen(dynamic obj)
        {
            var str = _IMyKaoHeBiao.getGongZuoChengDanRen(obj.username.ToString());
            //return JsonConvert.DeserializeObject<List<Domain_JiaoZhiGong>>(str);
            return str;
        }

        /// <summary>
        /// 保存考核表二级内容
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public dynamic saveKaoHeNeiRongTow(dynamic obj)
        {
            Domain_PersonalContent model = JsonConvert.DeserializeObject<Domain_PersonalContent>(obj.model.ToString());
            var id = _IMyKaoHeBiao.CreateMyKaoHeBiaoContent(model, int.Parse(obj.mkhbid.ToString()),
                int.Parse(obj.pathid.ToString()), obj.username.ToString());

            dynamic result = new { id = id };
            return result;

        }

        /// <summary>
        /// 删除考核表二级内容
        /// </summary>
        /// <param name="obj"></param>
        public void delKaoHeNeiRongTow(dynamic obj)
        {
            _IMyKaoHeBiao.DelMyKaoHeBiaoContent((int)obj.towid, int.Parse(obj.mkhbid.ToString()), int.Parse(obj.pathid.ToString()), obj.username.ToString());
        }

        /// <summary>
        /// 更新考核表二级内容
        /// </summary>
        /// <param name="obj"></param>
        public void updateKaoHeNeiRongTow(dynamic obj)
        {
            Domain_PersonalContent tow = JsonConvert.DeserializeObject<Domain_PersonalContent>(obj.model.ToString());
            _IMyKaoHeBiao.UpdateMyKaoHeBiaoContent(tow, int.Parse(obj.mkhbid.ToString()),
                int.Parse(obj.pathid.ToString()), obj.username.ToString());
        }

        /// <summary>
        /// 获取没有分配周数的月工作计划
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost, WriteFileExceptionAspect]
        public Domain_MyKaoHeBiao GetWeiFenPeiYueJiHua(dynamic obj)
        {
            var model = _IMyKaoHeBiao.获取没有分配周数的月工作计划(int.Parse(obj.khbid.ToString()));
            return JsonConvert.DeserializeObject<Domain_MyKaoHeBiao>(model);
        }

        /// <summary>
        /// 获取已分配周数的周计划
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost, WriteFileExceptionAspect]
        public Domain_MyKaoHeBiao GetYiFenPeiZhouJiHua(dynamic obj)
        {
            var model = _IMyKaoHeBiao.获取已分配周数的周计划(int.Parse(obj.khbid.ToString()), (int)obj.周数);
            return JsonConvert.DeserializeObject<Domain_MyKaoHeBiao>(model);
        }

        /// <summary>
        /// 生成单条周计划
        /// </summary>
        /// <param name="obj"></param>
        public void CreateDanTiaoZhouJiHua(dynamic obj)
        {
            _IMyKaoHeBiao.生成单条周计划((int)obj.khbid, (int)obj.twoid, (int)obj.周数);
        }

        /// <summary>
        /// 取消单条周计划
        /// </summary>
        /// <param name="obj"></param>
        public void DelDanTiaoZhouJiHua(dynamic obj)
        {
            _IMyKaoHeBiao.取消单条周计划((int)obj.khbid, (int)obj.twoid, (int)obj.周数);
        }

        public void UpdateGZJHWCQK(dynamic obj)
        {
            _IMyKaoHeBiao.更新工作计划完成情况((int)obj.khbid, (int)obj.twoid, (bool)obj.flag);

        }

        public string getHuoQuMeiJu()
        {
            var dict = Enum.GetValues(typeof(总结类型))
                .Cast<总结类型>()
                .ToDictionary(t => (int)t, t => t.ToString());
            return JsonConvert.SerializeObject(dict, Formatting.Indented);

        }

        public void CreateGongZuoZongJie(dynamic obj)
        {
            _IMyKaoHeBiao.添加月工作总结(int.Parse(obj.khbid.ToString()), int.Parse(obj.zongjielx.ToString()), obj.zongjienr.ToString());
        }

        public void DelGongZuoZongJie(dynamic obj)
        {
            _IMyKaoHeBiao.删除月工作总结(int.Parse(obj.khbid.ToString()), int.Parse(obj.zjid.ToString()));
        }

        public void UpdateGongZuoZongJie(dynamic obj)
        {
            _IMyKaoHeBiao.修改月工作总结(int.Parse(obj.khbid.ToString()), int.Parse(obj.zjtowid.ToString()), obj.zongjienr.ToString(), int.Parse(obj.zongjielx.ToString()));
        }

        public zongjietwo getGongZuoZongJie(int khbid, int 总结Id)
        {
            var model = _IMyKaoHeBiao.获取月工作总结(khbid, 总结Id);
            return JsonConvert.DeserializeObject<zongjietwo>(model);
        }
        [HttpPost, WriteFileExceptionAspect]
        public zongjieone getGongZuoZongJies(dynamic obj)
        {
            var model = _IMyKaoHeBiao.获取月工作总结集(int.Parse(obj.khbid.ToString()));
            return string.IsNullOrEmpty(model) ? new zongjieone() : (zongjieone)JsonConvert.DeserializeObject<zongjieone>(model);
        }
    }
}
